﻿/*
 Plugin for SmallJS toolkit
 Copyright (c) 2013 Walter M. Soto Reyes
 http://www.smalljs.com or https://smalljs.codeplex.com
 License: https://smalljs.codeplex.com/license
*/
(function ($) {
    var password = {
            variables: {
                lowerCase: 'abcdefghijklmnopqrstuvwxyz',
                upperCase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
                numbers: '1234567890',
                specialChars: '#$!?|',
                lifeTime: 365,
                label: {
                    excellent: 'Excellent',
                    strong: 'Strong',
                    medium: 'Medium',
                    weak: 'Weak',
                    reallyWeak: 'Really Weak',
                    colors: {
                        excellent: '#336600',
                        strong: 'green',
                        medium: '#B9B900',
                        weak: 'red',
                        reallyWeak: 'red'
                    }
                }
            },
        calculateStrength: function (pwd) {
            var strengthSet = {
                result: '',
                guardTime: 0,
                percentage: 0,
                color: ''
            };
            var _contains = function (pwd, required) {
                var i = 0;
                for (i = 0; i < pwd.length; i++) {
                    var current = pwd.charAt(i);
                    if (required.contains(current)) {
                        return true;
                    }
                }
                return false;
            };
            var total = 0;
            if (_contains(pwd, password.variables.lowerCase)) {
                total += 26
            }
            if (_contains(pwd, password.variables.upperCase)) {
                total += 26;
            }
            if (_contains(pwd, password.variables.numbers)) {
                total += 10;
            }

            if (_contains(pwd, password.variables.specialChars)) {
                total += password.variables.specialChars.length;
            }
            var combinations = Math.pow(total, pwd.length);
            strengthSet.guardTime = ((combinations / 200) / 2) / 86400; //(days it takes to hack the password) = (time is seconds)/86400
            strengthSet.percentage = strengthSet.guardTime / password.variables.lifeTime;
            strengthSet.result = (function (p) {
                if (p > 20000) {
                    return password.variables.label.excellent;
                }
                if (p > 20) {
                    return password.variables.label.strong;
                }
                if (p > 1) {
                    return password.variables.label.medium;
                }
                if (p > 0.01) {
                    return password.variables.label.weak;
                }
                return password.variables.label.reallyWeak;
            })(strengthSet.percentage);

            strengthSet.color = (function (r) {
                var color = password.variables.label.colors.reallyWeak;
                switch (r) {
                    case password.variables.label.excellent:
                        color = password.variables.label.colors.excellent;
                        break;
                    case password.variables.label.reallyWeak:
                        color = password.variables.label.colors.reallyWeak;
                        break;
                    case password.variables.label.weak:
                        color = password.variables.label.colors.weak;
                        break;
                    case password.variables.label.medium:
                        color = password.variables.label.colors.medium;
                        break;
                    case password.variables.label.strong:
                        color = password.variables.label.colors.strong;
                        break;

                }
                return color;
            })(strengthSet.result);

            return strengthSet;
        }
    };


    var passwordStrength = function (displayLabel) {
        ///<summary>
        /// Calculate password strength
        ///</summary>
        ///<param name="displayLabel" type="String">
        /// Label were the strength category will be displayed (Ex. strong, weak, medium, etc.)
        ///</param>
        ///	<returns type="this" />
        this.forEach(function () {
            (function (id, g) {
                $(id).on('keyup', function () {
                    if (typeof (displayLabel) !== 'undefined') {
                        var result = password.calculateStrength($(id).get[0].value).result;
                        var color = password.calculateStrength($(id).get[0].value).color;
                        $(displayLabel).style('color:' + color);
                        $(displayLabel).text(result);
                        if ($(id).text().length < 1) {
                            $(displayLabel).text('');
                        }
                    }
                });
            })(this);
        });

        return this;
    };


    smalljs.plugin({
        passwordStrength: passwordStrength
    });

})(sj);